﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using KqAutoDesign.Models;
using static System.Net.Mime.MediaTypeNames;

namespace KqAutoDesign.SonViews
{
    /// <summary>
    /// WorkFlowCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class WorkFlowCtrl : UserControl
    {
        public WorkFlowCtrl()
        {
            InitializeComponent();
        }

        public object Mp
        {
            get { return (object)GetValue(MpProperty); }
            set { SetValue(MpProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MpProperty =
            DependencyProperty.Register("Mp", typeof(object), typeof(WorkFlowCtrl), new PropertyMetadata(0,OnValueC));

        private static void OnValueC(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is WorkFlowCtrl wfc&&e.NewValue is TaskInfo tski)
            {
                WorkFlowCtrlVM vm = new WorkFlowCtrlVM();
                vm.Task = tski;
                wfc.SetData(vm);
            }
        }

        public void SetData(WorkFlowCtrlVM vm)
        {
            DataContext = vm;
            Draw();
        }

        private void Draw()
        {
            if(DataContext is WorkFlowCtrlVM vm)
            {
                double xPosition = 10.0;
                for (int i=0; i<vm.Task.WorkFlow.Modules.Count;i++)
                {
                    var module = vm.Task.WorkFlow.Modules[i];
                    ModuleCtrlVM ctvm = new ModuleCtrlVM();
                    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource = new Uri(Environment.CurrentDirectory + @"\Resources\Icons\"+module.Icon, UriKind.Absolute);
                    // CacheOption用于控制图像数据的缓存行为
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    img.Source = bitmap;
                    ctvm.Img = img;
                    ctvm.ModuleName = module.Name;
                    ctvm.Id = module.Id;
                    ModuleCtrl moduleCtrl = new ModuleCtrl(ctvm);
                    moduleCtrl.Width = 100.0;
                    moduleCtrl.Height = 80;
                    moduleCtrl.ModuleClicked += ModuleCtrl_ModuleClicked;
                    canvas.Children.Add(moduleCtrl);
                    Canvas.SetLeft(moduleCtrl, xPosition);
                    Canvas.SetTop(moduleCtrl, 30);
                    xPosition += moduleCtrl.Width;
                    if(i<=vm.Task.WorkFlow.Modules.Count-2)
                    {
                        Line line = new Line();
                        line.X1 = xPosition;
                        line.Y1 = canvas.Height / 2+10;
                        line.X2 = xPosition + 80;
                        line.Y2 = canvas.Height / 2+10;
                        line.Stroke = new SolidColorBrush(Colors.Red);
                        line.StrokeThickness = 2;
                        canvas.Children.Add(line);
                        xPosition += 80;
                    }
                } 
            }
        }

        private void ModuleCtrl_ModuleClicked(int id)
        {
            if(DataContext is WorkFlowCtrlVM vm)
            {
                vm.SelectedModule = vm.Task.WorkFlow.Modules.FirstOrDefault(x=>x.Id == id);
            }
        }

        private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
        {

        }

        private void NumberValidationTextBox(object sender, KeyEventArgs e)
        {
            var textBox = sender as TextBox;

            if ((e.Key == Key.OemPeriod && textBox.Text.Contains(".")) || e.Key == Key.OemPeriod && string.IsNullOrWhiteSpace(textBox.Text))
            {
                e.Handled = true;
                return;
            }
            ;

            if (!((e.Key >= Key.D0 && e.Key <= Key.D9)
                || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.OemPeriod
                || e.Key == Key.Tab || e.Key == Key.OemBackTab
                || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
                || (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.A))
            {
                if (e.KeyboardDevice.Modifiers != ModifierKeys.Control)
                {
                    e.Handled = true;
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(sender is Button btn&& btn.DataContext is GroupScriptParam gsp)
            {
                gsp.ScriptParam.Value = gsp.ScriptParam.DefaultValue;
            }
        }

        private void OpenDic_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button btn && btn.DataContext is GroupScriptParam gsp)
            {
                ParamDictionaryWd wd = new ParamDictionaryWd();
                if(wd.ShowDialog()==true)
                {
                    gsp.ScriptParam.Value = wd.Gsp?.ScriptParam.Value;
                }
            }
            
        }
    }
}
